package com.ld.businesscard_manage.service.impl;

import com.alibaba.excel.EasyExcel;
import com.ld.businesscard_manage.DAO.BusinessCardDAO;
import com.ld.businesscard_manage.DAO.RecyleDAO;
import com.ld.businesscard_manage.bean.AllCardsExcel;
import com.ld.businesscard_manage.bean.BusinessCard;
import com.ld.businesscard_manage.bean.User;
import com.ld.businesscard_manage.service.BusinessCardService;
import com.ld.businesscard_manage.utils.ExcelUtil;

import java.util.Date;
import java.util.List;

public class BusinessCardServiceImpl implements BusinessCardService {
	private BusinessCardDAO businessCardDAO = new BusinessCardDAO();
	private RecyleDAO recyleDAO = new RecyleDAO();

	@Override
	public int addCard(BusinessCard card) {
		return businessCardDAO.update("insert into businesscard(name,sex,account,password,email) values(?,?,?,?,?)",card.getName(),card.getSex(),card.getAccount(),card.getPassword(),card.getEmail());
	}

	@Override
	public int deleteCardById(Integer id) {
		return businessCardDAO.update("update businesscard set valid = 1 where id = ?",id);
	}

	@Override
	public int updateCard(BusinessCard card) {
		return businessCardDAO.update("update businesscard set name = ?,sex = ?,account = ?,password = ?,email = ?,valid = ? where id = ?",card.getName(),card.getSex(),card.getAccount(),card.getPassword(),card.getEmail(),card.getValid(),card.getId());
	}

	@Override
	public List<BusinessCard> getAllCards() {
		return businessCardDAO.queryMulti("select * from businesscard",BusinessCard.class);
	}

	@Override
	public List<BusinessCard> getCardsByLike(String name) {
		return businessCardDAO.queryMulti("select * from businesscard where name  like '%" + name + "%'",BusinessCard.class);
	}

	@Override
	public List<BusinessCard> getValidCards() {
		return businessCardDAO.queryMulti("select * from businesscard where valid = 0",BusinessCard.class);
	}

	@Override
	public BusinessCard getCardById(Integer id) {
		return businessCardDAO.querySingle("select * from businesscard where id = ?",BusinessCard.class, id);
	}
	public int deletedCardById(Integer id) {
		return businessCardDAO.update("update businesscard set valid = 2 where id = ?",id);
	}

	@Override
	public int recoverCard(Integer id) {
		return businessCardDAO.update("update businesscard set valid = 0 where id = ?",id);
	}

	@Override
	public List<BusinessCard> getAllValidCards() {
		return businessCardDAO.queryMulti("select * from businesscard where valid = 0",BusinessCard.class);
	}

	@Override
	public List<BusinessCard> getMyCards(String account) {
		return businessCardDAO.queryMulti("select * from businesscard where account = ?",BusinessCard.class,account);
	}

	@Override
	public List<BusinessCard> getMyValidCards(String account) {
		return businessCardDAO.queryMulti("select * from businesscard where account = ? and valid = 0",BusinessCard.class,account);
	}

	@Override
	public List<BusinessCard> getMyCardsByLike(String name, String account) {
		return businessCardDAO.queryMulti("select * from businesscard where account = ? and name  like '%" + name + "%'",BusinessCard.class,account);
	}

	//@Override
	public int changeCardsByAccount(User user, String account) {
		return businessCardDAO.update("update businesscard set account = ?,password = ?where account = ?",user.getUsername(),user.getPassword(),account);
	}

	@Override
	public int deleteUser(String account) {
		int i = 0;
		List<BusinessCard> myCards = getMyCards(account);
		if(!myCards.isEmpty()){
			for (BusinessCard myCard : myCards) {
				if (myCard != null) {
					if(myCard.getValid() == 1){
						recyleDAO.update("delete from recyle where `businesscard_id` = ?",myCard.getId());
					}
					deletedCardById(myCard.getId());
					i ++;
				}
			}
		}
		return i;
	}

	@Override
	public void exportAllCardsToExcel(List<BusinessCard> cards) {
			String fileName = "d://" + System.currentTimeMillis() + ".xlsx";
			EasyExcel.write(fileName, AllCardsExcel.class).sheet("所有名片信息").doWrite(ExcelUtil.data(cards));
	}
}
